from django.db import models
from utils.models import CoreModel
from system.settings.utils import AppSetting
from system.host.models import Host
from django.conf import settings

table_prefix = settings.TABLE_PREFIX  # 数据库表名前缀


class ScriptGroup(CoreModel):
    name = models.CharField(max_length=64)
    parent = models.ForeignKey(to='self', to_field='id', on_delete=models.CASCADE, related_name='self_parent',
                               verbose_name='上级节点', null=True, blank=True)
    sort_id = models.IntegerField(default=0)

    class Meta:
        db_table = table_prefix + 'script_groups'
        verbose_name = '脚本组管理'
        ordering = ('-sort_id',)


class Script(CoreModel):
    name = models.CharField(max_length=64, verbose_name='脚本名称')
    project = models.CharField(max_length=64, verbose_name='项目', null=True)
    language = models.CharField(max_length=64, verbose_name='语言', null=True)
    parameters = models.TextField(verbose_name='参数')
    command = models.TextField(verbose_name='执行命令')
    log_command = models.TextField(verbose_name='查看日志命令', null=True)
    host = models.ForeignKey(Host, to_field='id', verbose_name='执行主机', on_delete=models.CASCADE)
    group = models.ForeignKey(ScriptGroup, to_field='id', verbose_name='脚本组', on_delete=models.CASCADE)

    @property
    def private_key(self):
        return AppSetting.get('private_key')

    @staticmethod
    def make_parameters(parameters):
        tmp = []
        for p in parameters:
            if p['type'] == 'option':
                p['option'] = p['option'].split('\n')
            tmp.append(p)
        return tmp

    class Meta:
        db_table = table_prefix + 'script'
        verbose_name = '脚本列表'
        verbose_name_plural = verbose_name


class ScriptRecord(CoreModel):
    build_id = models.CharField(max_length=32, verbose_name='构建ID')
    parameters = models.TextField(verbose_name='参数')
    state_choice = [(0, '成功'), (1, '失败'), (2, '发布中')]
    state = models.IntegerField(choices=state_choice, default=0, verbose_name='构建状态')
    log = models.TextField(verbose_name='构建日志')
    token = models.CharField(max_length=32, null=True, verbose_name='构建token')
    build_at = models.CharField(max_length=64, verbose_name='上次构建时间')
    build_time = models.CharField(max_length=64, verbose_name='上次构建时长', null=True)
    extend = models.CharField(max_length=64, verbose_name='额外参数', null=True)  # 用于判断是否是测试服更新
    build_by = models.CharField(max_length=64, verbose_name='构建人', null=True)
    task = models.ForeignKey(Script, to_field='id', verbose_name='构建任务', on_delete=models.CASCADE)

    # 获取下一次的build_id
    @staticmethod
    def make_build_id(script):
        record = ScriptRecord.objects.filter(task=script).order_by('-created_at').first()
        return record.build_id + 1 if record else 1

    class Meta:
        db_table = table_prefix + 'script_record'
        verbose_name = '脚本执行记录表'
        verbose_name_plural = verbose_name


class GameVersion(CoreModel):
    game_type = models.CharField(max_length=32, verbose_name='游戏项目')
    language = models.CharField(max_length=32, verbose_name='语言')
    version = models.CharField(max_length=64, verbose_name='版本')
    suffix = models.CharField(max_length=32, verbose_name='版本后缀')

    class Meta:
        db_table = table_prefix + 'game_version'
        verbose_name = '游戏版本表'
        verbose_name_plural = verbose_name
